onload = () => {
  $('#headerDivB').text('项目详情')

  project = $util.getPageParam('seeProject')
  console.log(project, 'project')
  $('#projectName').text(project.projectName)
  $('#createTime').text(project.creationDate)
  $('#personInCharge').text(project.lastUpdatedBy)
  $('#projectDescription').text(project.projectContent)
  fetchProjectInfo(project.projectName)
}

const fetchProjectInfo = (projectName) => {
  let params = {
    projectName:projectName
  }
  $.ajax({
    url: API_BASE_URL + '/searchQuestionnaireList',
    type: "POST",
    data: JSON.stringify(params),
    dataType: "json",
    contentType: "application/json",
    success(res) {
      $('#table #tbody').html('')
      questionnaireList = res.data
      res.data.map((item, index) => {
        var time=new Date(+new Date(new Date(item.startTime).toJSON())+8*3600*1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
        // var stopTime=new Date(+new Date(new Date(item.stopTime).toJSON())+8*3600*1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
        $('#table #tbody').append(`
          <tr>
            <td>${index + 1}</td>
            <td>${item.name}</td>
            <td>${item.startTime}</td>
            <td>
              <button type="button" class="btn btn-link" onclick="seeQuestionnaire('${item.id}')">预览</button>
              <button type="button" class="btn btn-link" onclick="updateQuestionnaire('${item.id}')">发布</button>
              <button type="button" class="btn btn-link" onclick="deleteQuestionnaire('${item.id}')">删除</button>
              <button type="button" class="btn btn-link" onclick="statistics('${item.id}')">统计</button>
            </td>
          </tr>
        `)
      })
    }
  })
}

const seeQuestionnaire = () => {
  location.href = "/pages/answerSheet/index.html"
}

const deleteQuestionnaire = (id) => {
  let params = {
    id:id
  }
  $.ajax({
    url: API_BASE_URL + '/deleteQuestionnaire',
    type: "POST",
    data: JSON.stringify(params),
    dataType: "json",
    contentType: "application/json",
    success(res) {
      if (res.code === '666') {
        alert(res.message)
        location.href = "/pages/seeProject/index.html"
      } else {
        alert(res.message)
      }
    }
  })
}

const updateQuestionnaire = (id) => {
  let params = {
    id:id
  }
  $.ajax({
    url: API_BASE_URL + '/updateQuestionnaire',
    type: "POST",
    data: JSON.stringify(params),
    dataType: "json",
    contentType: "application/json",
    success(res) {
      alert("问卷发布成功,链接："+"127.0.0.1/8085/controller/AnswerController?id=5")
      location.href = "/pages/seeProject/index.html"
    }
  })
}

const statistics = () => {
  location.href = "/pages/answerCount/index.html"
}